package com.wh.wisdomsite.xbox.client.service.zywl;

import com.alibaba.fastjson.JSONObject;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.common.annotation.NeedInSessionType;
import com.wh.wisdomsite.xbox.common.annotation.ServiceMethod;
import com.wh.wisdomsite.xbox.common.annotation.ServiceProvider;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.pojo.zywl.GetPushDataIn;
import com.wh.wisdomsite.xbox.pojo.zywl.GetPushDataOut;
import com.wh.wisdomsite.xbox.pojo.zywl.RefundTicketIn;
import com.wh.wisdomsite.xbox.pojo.zywl.UseTicketIn;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.zywl.service.IZywlUserService;
import com.wh.wisdomsite.xbox.zywl.service.IZywlUserTicketService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 遵义微乐票据接口 Service
 * @author 丁奕
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.client.service.zywl
 * @date 2018年12月26日 11:36
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@ServiceProvider()
public class ZywlTicketService {

    private static final Log logger = LogFactory.getLog(ZywlTicketService.class);

    @Autowired
    private IZywlUserService zywlUserService;

    @Autowired
    private IZywlUserTicketService zywlUserTicketService;

    /**
     * 获取推送票务信息
     * @param in
     * @return
     */
    @ServiceMethod(method = "zywl.ticket.getPushData", title = "获取票务", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult getPushData(GetPushDataIn in){
        CommonResult commonResult = new CommonResult();
        Map<String, Object> resultMap = new HashMap<>();
        try{
            logger.info("获取票务->>zywl.ticket.getPushData->>开始");
            GetPushDataOut getPushDataOut = new GetPushDataOut();
            getPushDataOut.setUserTicketList(in.getUserTicketList());
            getPushDataOut.getZywlUser().setIdentityId(in.getIdentityId());
            getPushDataOut.getZywlUser().setIdcardId(in.getIdcardId());
            getPushDataOut.getZywlUser().setCurrent_time(StringUtils.isNotEmpty(in.getCurrent_time() + "") ? Long.parseLong(in.getCurrent_time() + "") : 0);
            getPushDataOut.getZywlUser().setFaceImage(in.getFaceImage());
            getPushDataOut.getZywlUser().setState(in.getState());
            zywlUserService.synGetPushDataOut(getPushDataOut);
            commonResult.setCode(Constants.CODE_SUCCESS);
            commonResult.setMessage("同步票务信息成功");
            resultMap.put("sCode", 1);
            resultMap.put("sMsg", true);
            logger.info("获取票务信息->>zywl.ticket.getPushData->>结束");
        } catch (Exception e){
            e.printStackTrace();
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage("获取票务信息异常");
            resultMap.put("sCode", -1);
            resultMap.put("sMsg", false);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            logger.error("获取票务信息->>zywl.ticket.getPushData->>异常："+e.getMessage());
        }
        resultMap.put("sTimes", new Date().getTime());
        commonResult.setResult(resultMap);
        return commonResult;
    }

    /**
     * 用户退票
     * @param in
     * @return
     */
    @ServiceMethod(method = "zywl.ticket.refundTicket", title = "用户退票", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult refundTicket(RefundTicketIn in){
        CommonResult commonResult = new CommonResult();
        Map<String, Object> resultMap = new HashMap<>();
        try{
            logger.info("用户退票->>zywl.ticket.getPushData->>开始");
            //检查票务ID是否为空
            if(StringUtils.isEmpty(in.getUserTicketId())){
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage("票务ID不位空");
                return commonResult;
            }
            commonResult = zywlUserTicketService.refundTicket(in.getUserTicketId());
            if(commonResult.getCode().equals(Constants.CODE_SUCCESS)){
                //退票成功
                resultMap.put("sCode", 1);
                resultMap.put("sMsg", true);
            }else{
                //退票失败
                resultMap.put("sCode", -1);
                resultMap.put("sMsg", false);
            }
            logger.info("用户退票->>zywl.ticket.getPushData->>结束");
        } catch (Exception e){
            e.printStackTrace();
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage("用户退票异常");
            resultMap.put("sCode", -1);
            resultMap.put("sMsg", false);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            logger.error("用户退票->>zywl.ticket.getPushData->>异常："+e.getMessage());
        }
        resultMap.put("sTimes", new Date().getTime());
        commonResult.setResult(resultMap);
        return commonResult;
    }

    /**
     * 使用票务
     * @param in
     * @return
     */
    @ServiceMethod(method = "zywl.ticket.useTicket", title = "使用票务", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult useTicket(UseTicketIn in){
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("使用票务信息->>zywl.ticket.useTicket->>开始");
            if(StringUtils.isEmpty(in.getUserTicketId())){
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage("票务编号不能为空");
                return commonResult;
            }
            commonResult = zywlUserTicketService.useTicket(in);
            logger.info("使用票务信息->>zywl.ticket.useTicket->>结束");
        } catch (Exception e){
            e.printStackTrace();
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage("使用票务异常");
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            logger.error("使用票务信息->>zywl.machine.useTicket->>异常："+e.getMessage());
        }
            return commonResult;
    }

    /**
     * 使用身份证验证
     * @param in
     * @return
     */
    @ServiceMethod(method = "zywl.ticket.useIdCard", title = "使用身份证验证", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult useIdCard(UseTicketIn in){
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("使用身份证验证->>zywl.ticket.useIdCard->>开始");
            if(StringUtils.isEmpty(in.getIdCard())){
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage("身份证号不能为空");
                return commonResult;
            }
            in.setPassType("I");
            commonResult = zywlUserTicketService.useIdCard(in);
            logger.info("使用身份证验证->>zywl.ticket.useIdCard->>结束");
        } catch (Exception e){
            e.printStackTrace();
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage("使用身份证验证异常");
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            logger.error("使用身份证验证->>zywl.machine.useIdCard->>异常："+e.getMessage());
        }
        return commonResult;
    }

}
